package com.aidex.system.service.impl;

import java.util.Date;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.DeptUtils;
import com.aidex.framework.cache.UserUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.system.mapper.ApiLogMapper;
import com.aidex.system.domain.ApiLog;
import com.aidex.system.service.ApiLogService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 接口日志Service业务层处理
 * @author jmy
 * @email jmyinjg@163.com
 * @date 2021-11-14
 */
@Service
@Transactional(readOnly = false)
public class ApiLogServiceImpl extends BaseServiceImpl<ApiLogMapper, ApiLog> implements ApiLogService {
    private static final Logger log = LoggerFactory.getLogger(ApiLogServiceImpl.class);

    /**
     * 获取单条数据
     *
     * @param apiLog 接口日志
     * @return 接口日志
     */
    @Override
    public ApiLog get(ApiLog apiLog) {
        ApiLog dto = super.get(apiLog);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 接口日志id
     * @return 接口日志
     */
    @Override
    public ApiLog get(String id) {
        ApiLog dto = super.get(id);
        return dto;
    }


    /**
     * 查询接口日志列表
     *
     * @param apiLog 接口日志
     * @return 接口日志
     */
    @Override
    public List<ApiLog> findList(ApiLog apiLog) {
        return super.findList(apiLog);
    }

    /**
     * 分页查询接口日志列表
     *
     * @param apiLog 接口日志
     * @return 接口日志
     */
    @Override
    public PageInfo<ApiLog> findPage(ApiLog apiLog) {
        return super.findPage(apiLog);
    }

    /**
     * 保存接口日志
     *
     * @param apiLog
     * @return 结果
     */
    @Override
    public boolean save(ApiLog apiLog) {
        return super.save(apiLog);
    }


    /**
     * 删除接口日志信息
     *
     * @param apiLog
     * @return 结果
     */
    @Override
    public boolean remove(ApiLog apiLog) {
        return super.remove(apiLog);
    }

    /**
     * 批量删除接口日志
     *
     * @param ids 需要删除的接口日志ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteApiLogByIds(String[] ids) {
        return mapper.deleteApiLogByIds(ids);
    }

    /*@Override
    public ApiLog findByData(Date apiDate) {
        return mapper.findByData(apiDate);
    }*/

    @Override
    public int updateError(String apiType) {
        ApiLog apiLog = new ApiLog();
        apiLog.setLogDate(new Date());
        apiLog.setApiType(apiType);
        try {
            apiLog = mapper.findByData(apiLog);
            if (apiLog == null) {
                apiLog = new ApiLog();
                apiLog.setLogDate(new Date());
                apiLog.setApiType(apiType);
                apiLog.setApiScuessnum(0L);
                apiLog.setApiErrornum(0L);
                save(apiLog);
            }
            return mapper.updateError(apiLog);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateScuess(String apiType) {
        ApiLog apiLog = new ApiLog();
        apiLog.setLogDate(new Date());
        apiLog.setApiType(apiType);
        try {
            apiLog = mapper.findByData(apiLog);
            if (apiLog == null) {
                apiLog = new ApiLog();
                apiLog.setLogDate(new Date());
                apiLog.setApiType(apiType);
                apiLog.setApiScuessnum(0L);
                apiLog.setApiErrornum(0L);
                save(apiLog);
            }
            return mapper.updateScuess(apiLog);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 0;
    }
}